﻿using UnityEngine;

namespace HEFramework
{
    public static class BuildingViewSystem
    {
        [EntitySystem]
        public class BuildingViewAwakeSystem : AwakeSystem<BuildingView>
        {
            protected override void Awake(BuildingView _self)
            {
                var pc = _self.GetSceneChild<PrefabComponent>();
                var dc = _self.GetSceneChild<DataTableComponent>();
                var building = _self.GetParent<Building>();
                if (pc == null || dc == null || building == null)
                {
                    return;
                }

                _self.ID = building.ID;
                var data = dc.LoadDataTable<DRAttribute>().GetData(_self.ID);
                pc.GetPrefabAsync(data.ABPath, data.ABName, _ =>
                {
                    if (_self.IsDisposed)
                    {
                        pc.Recycle(data.ABPath, data.ABName, _);
                    }
                    else
                    {
                        _self.Behaviour = _.GetComponent<TransformBehaviour>();
                    }
                }, false);
            }
        }

        [EntitySystem]
        public class BuildingViewUpdateSystem : UpdateSystem<BuildingView>
        {
            protected override void Update(BuildingView _self)
            {
                _self.LookAtCamera();
                _self.Update_Pos();
            }
        }

        [EntitySystem]
        public class BuildingViewDestroySystem : DestroySystem<BuildingView>
        {
            protected override void Destroy(BuildingView _self)
            {
                if (_self.Behaviour == null)
                {
                    return;
                }

                var pc = _self.GetSceneChild<PrefabComponent>();
                var dc = _self.GetSceneChild<DataTableComponent>();
                var building = _self.GetParent<Building>();
                if (pc == null || dc == null || building == null)
                {
                    GameObject.Destroy(_self.Behaviour.gameObject);
                    _self.Behaviour = null;
                    return;
                }

                //回收
                var data = dc.LoadDataTable<DRAttribute>().GetData(building.ID);
                pc.Recycle(data.ABPath, data.ABName, _self.Behaviour.gameObject);
                _self.Behaviour = null;
            }
        }

        public static void LookAtCamera(this BuildingView _self)
        {
            if (_self.Behaviour == null)
            {
                return;
            }

            var gc = _self.GetSceneChild<GameCamera>();
            if (gc == null)
            {
                return;
            }

            gc.SetGameObjectRotateToCamera(_self.Behaviour.GetTransform("Transform_Body"));
        }

        public static void Update_Pos(this BuildingView _self)
        {
            if (_self.Behaviour == null)
            {
                return;
            }

            var tc = _self.GetParentChild<TransformComponent>();
            if (tc == null)
            {
                return;
            }

            _self.Behaviour.gameObject.transform.position = tc.P.ToVector();

            if (!_self.Behaviour.gameObject.activeSelf)
            {
                _self.Behaviour.gameObject.SetActive(true);
            }
        }
    }
}